The purpose of this lab is to:
We have talked about dictionaries n class. Here is a brief reminder of the main properties of dictionaries.
Ages = {} # sets Ages to be an empty dictionary Ages["Hermione"] # returns the values associated with "Hermione", presumbly
# her age. Throws an error if "Hermione" is not a key. Ages["Hermione" = 18 # Makes "Hermione" a key and associates 18 with it. del Ages["Hermione"] # removes key "Hermione" and the value associated with it. Ages.keys()) # returns a "view" of the keys of Ages. You can treat this # like a list of the keys. len(Ages) # returns the number of keys in Ages for person in Ages : # Iterates over the keys in Ages.
Consider the following text:
Question: Whether nobler mind suffer Slings arrows outrageous fortune Take arms against sea troubles By opposing them.You probably recognized this as a condensed version of the first few lines of Hamlet's famous "To be, or not to be" soliloquy. The original unedited text is:
To be, or not to be -- that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them.
The former was produced by finding the 30 most commonly used words in the speech and removing them. Your first challenge is to write a program called distill.py that prompts the user for the name of a text file and a number n, and prints the contents of that text file with the n most common words removed.
The details of implementing a solution are up to you, but here is a suggested outline of how to approach the problem. As usual, think about the 6 steps of program development, and test each piece as you go.
A set is another built-in data structures supported by Python for the mathematical notion of a set, i.e. a collection of elements. Unlike a dictionary, the elements in a set don't have values associated with them. You could simulate a set using a dictionary, by adding a key for each element, and setting that key's value to something arbitrary, like 0, or an empty string, or none. That said, if you don't have data associated with each element, and simply whant to keep track of a set of items, using a set is the way to go.
Like dictionarys (and unlike lists), sets are not ordered, but testing membership and addinging or removing elements is very fast. Sets do not store duplicate elements: adding an element to a set that already contains that element has no effect.
Here are some examples of syntax involving sets.
team = set() # makes a set with 0 elements team = {"kirk", "spock"} # makes a set with 2 elements len(team) # 2 team.add("bones") # adds "bones" to team team.remove("kirk") # removes "kirk" from team for p in team : # iterates through elements of team "bones" in team # True "malcolm" in team # False "river" not in team # True
An anagram is just a rearrangement of the letters in the word to form another word or words. For example, here are some anagrams for the phrase "oberlin student":
let none disturb
run no bed titles
let us not rebind
trust line on bed
but not red lines
bound in letters
let in; runs to bed
For this part of the lab, you will write a program called anagrams.py that reads in a file so it knows what strings are words in English, and then reads phrases from the user and prints anagrams for them. Your program should prompt the user for the dictionary file, then go into a loop reading strings and printing anagrams for them.
contains("zombiepig", "bozo") # returns False, "" contains("zombiepig", "biz") # returns True, "omepig"
You might be wondering why we're passing around the variable sofar. Indeed, when we want to find the anagrams of a string given by the user, we'll pass in an empty list. However, that list will be critical for making use of recursion. Let's look at an example to see why. Suppose we want to find anagrams of the word
robopirateTo do this we look through our wordlist for words that are contained in this string. The string "cat" doesn't appear in "robopirate", but "air" does. So one thing our function call will do is begin looking through the remainder of "robopirate" with "air" removed, looking for further anagrams. That is, it'll continue to look for strings contained in
robopteOur list includes "bro", which is contained in "robopte", so another recursive call will be made on the remains, namely "opte". Our wordlist contains "poet", leaving us with an empty string. At this point we've used up all the letters in the string, so we have an anagram, namely
air bro poetUnfortunately, if we want to print our anagram, we're in trouble, since we haven't kept a record of the previous words we found. (Why couldn't we have just printed words as we found them?) That's where sofar comes in. This list will track the words we've found so far in this particular branch of the recursion. That is,
grams("robopirate", words, [])will call (among other things)
grams("robopte", words, ["air"])which in turn calls
grams("opte", words, ["air", "bro"])which in turn calls
grams("", words, ["air", "bro", "poet"])which can now print the complete anagram.
With this in mind, we're ready to describe the overall structure of this function. We loop over every word w in our wordlist. For each word w that's found in our string s, we make a recursive call on the remainder of s, and with a new list, equal to the current list with w added on. We need to make a new list, because there is no easy way to remove w from the list when we move on to another word. If we recurse to the point where s is the empty string, we can just print the contents of sofar.
or ape orbit or orbit ape bro air poet bro poet air air bro poet air poet bro ape or orbit ape orbit or poet bro air poet air bro orbit or ape orbit ape or
It doesn't matter if your output is in another order. Notice that for any set of words that form an anagram, the program prints this set once for each possible ordering of the words. There are k! orderings for a set of k words. In this robopirate example there are two sets of words making the anagrams: {'or", 'ape', 'orbit'} and {'poet', 'air', 'bro'}. The twelve lines of output consist of each of these sets printed 6 times (since 6 =3!). When printing anagrams of "oberlin student" with the words2.txt dictionary, I had over 35,000 lines of output.
Here are some tests for the larger words2.txt file:
"frodo baggins" has anagrams that include "go for bad sign"
"hermione granger" has anagrams that include "ignore green harm"
"ron weasley" has anagrams that include "as we rely on"
"oberlin conservatory" has many anagrams, including "so convert one library", "naive err controls boy", "only recover into bars", "obtain no clever sorry", "lost, recover no binary", "boy never controls air", and "be sorry; naive control". Don't tell Dean Kalyn about that last one.
Add a "preprocessing" step: Instead of adding every string found in the word file to the set words, only add those that are contained in the user's input string. Fortunately you already have created a function that can help you out here.
If you followed the Honor Code in this assignment, create an HonorCodefile containing the text.
I affirm that I have adhered to the Honor Code in this assignment.
You now just need to electronically handin all your files. As a reminder
% cd # changes to your home directory % cd cs150 # goes to your cs150 folder % handin # starts the handin program # class is 150 # assignment is 8 # file/directory is lab08 % lshand # should show that you've handed in something
You can also specify the options to handin from the command line
% cd ~/cs150 # goes to your cs150 folder % handin -c 150 -a 8 lab08
distill.py anagrams.py README.txt (with the Honor Pledge)